

import 'package:flutter/material.dart';

import 'package:qianyi/network/http_entity.dart';
import 'package:qianyi/pages/home/index.dart';
import 'package:qianyi/pages/login/widgets/head.dart';
import 'package:qianyi/state/token.dart';
import 'package:qianyi/utils/throttle.dart';
import 'package:qianyi/wcao/ui/theme.dart';


import 'package:get/get.dart';

import '../../network/base_entity.dart';
import '../../network/http_config.dart';
import '../../network/http_manager.dart';

// HACK: 密码登录

class LoginPassword extends StatefulWidget {
  const LoginPassword({Key? key}) : super(key: key);

  @override
  State<LoginPassword> createState() => _LoginState();
}

class _LoginState extends State<LoginPassword> {
  TokenController _tokenController = Get.find<TokenController>();

  @override
  void initState() {
    // TODO: implement initState


  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset:false,
      body: SafeArea(
        child: InkWell(
          onTap: () {
            FocusManager.instance.primaryFocus?.unfocus();
          },
          child: Container(
            alignment: Alignment.centerLeft,
            padding: const EdgeInsets.symmetric(horizontal: 26),
            margin: const EdgeInsets.only(bottom: 56),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Head(),
                buildPassWord(),
                loginButton(),
                Container(
                  margin: const EdgeInsets.only(top: 16),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Opacity(
                        opacity: 0,
                        child: Text(
                          "忘记密码?",
                          style: TextStyle(
                            color: WcaoTheme.placeholder,
                            fontSize: 12,
                          ),
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          Navigator.pushReplacementNamed(
                              context, '/login/verify-code');
                        },
                        child: Container(
                          alignment: Alignment.center,
                          child: Text(
                            "免密登录",
                            style: TextStyle(
                              // color: WcaoTheme.primaryFocus,
                              // fontWeight: FontWeight.w500,
                            ),
                          ),
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          Get.toNamed('/verify-code/reset_password');
                        },
                        child: Text(
                          "忘记密码?",
                          style: TextStyle(
                            color: WcaoTheme.secondary,
                            fontSize: 12,
                          ),
                        ),
                      )
                    ],
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(top: 16),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        "登录即同意",
                        style: TextStyle(color: WcaoTheme.placeholder),
                      ),
                      InkWell(child: const Text('《用户协议》'), onTap: () {}),
                      Text(
                        "及",
                        style: TextStyle(color: WcaoTheme.placeholder),
                      ),
                      InkWell(child: const Text('《隐私政策》'), onTap: () {}),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  /// 密码登录
  Column buildPassWord() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Container(
          margin: const EdgeInsets.only(top: 12),
          child: Wrap(
            children: [
              Text(
                '密码',
                style: TextStyle(
                  fontSize: 14,
                  color: WcaoTheme.placeholder,
                  // fontWeight: FontWeight.w500,
                ),
              ),
            ],
          ),
        ),
        Container(
          margin: const EdgeInsets.only(top: 12),
          height: 50,
          child: TextField(
            controller: _tokenController.passwordTextController,
            keyboardType: TextInputType.visiblePassword,
            obscureText: true,
            enableSuggestions: false,
            autocorrect: false,
            decoration: InputDecoration(
              hintText: "请输入密码",
              contentPadding:
              const EdgeInsets.symmetric(vertical: 0, horizontal: 16),
              // enabledBorder: OutlineInputBorder(
              //   borderRadius: BorderRadius.circular(8),
              //   borderSide: BorderSide(
              //     color: WcaoTheme.outline,
              //     width: 2,
              //   ),
              // ),
              focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(
                  color: WcaoTheme.base, // 聚焦时下划线颜色
                  width: 1,
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }

  /// 登录按钮
  InkWell loginButton()  {
    return InkWell(
      onTap: () async{
        HudUtil.showBeautiful(context);
        Map<String,dynamic> params = {
          "username":_tokenController.usernameTextController.text,
          "password":_tokenController.passwordTextController.text
        };
        IBaseEntity? iBaseEntity = await HttpManager.getInstance().request(url: "/auth/login",method: Method.post,params: params) as HttpEntity;
        HttpEntity httpEntity = iBaseEntity as HttpEntity;
        // print('密码${_tokenController.passwordTextController.text}${_tokenController.usernameTextController.text}');
        await _tokenController.setLocal(httpEntity.oriData['data']);
        await _tokenController.getUserInfo();
        HudUtil.dismiss();
        // 使用pushAndRemoveUntil跳转回首页
        Navigator.pushAndRemoveUntil(
          context,
          MaterialPageRoute(builder: (context) => Home()), // 假设HomePage是您的首页
              (Route<dynamic> route) => true, // 移除所有之前的路由
        );
      },
      child: Container(
        margin: const EdgeInsets.only(top: 36),
        alignment: Alignment.center,
        width: double.infinity,
        height: 44,
        decoration: BoxDecoration(
          color: WcaoTheme.primary,
          borderRadius: WcaoTheme.radius,
        ),
        child: const Text(
          '登录',
          style: TextStyle(
              fontSize: 18, fontWeight: FontWeight.w500, color: Colors.white),
        ),
      ),
    );
  }
}
